﻿Imports System.Data
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class PaymentMethode
    Implements INotifyPropertyChanged

    Private _name, _text As String
    Private _reduction As Decimal
    Public Sub New()

    End Sub

    Public Sub New(Name As String, Text As String, Abzug As String)
        _name = Name
        _text = Text
        _reduction = Abzug
    End Sub

    Public Overridable Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            If _name <> value Then
                _name = value
                NotifyPropertyChanged("Name")
            End If
        End Set
    End Property

    Public Overridable Property Text As String
        Get
            Return _text
        End Get
        Set(value As String)
            If _text <> value Then
                _text = value
                NotifyPropertyChanged("Text")
            End If
        End Set
    End Property

    Public Overridable Property Reduction As Decimal
        Get
            Return _reduction
        End Get
        Set(value As Decimal)
            If _reduction <> value Then
                _reduction = value
                NotifyPropertyChanged("Reduction")
                NotifyPropertyChanged("ReductionString")
            End If
        End Set
    End Property

    Public Overridable Property ReductionString As String
        Get
            Return Math.Round(Reduction * 100, 2).ToString + " %"
        End Get
        Set(value As String)
            Try
                Reduction = CDec(value.Replace("%", "").Replace(" ", "")) / 100
            Catch ex As Exception
                Reduction = 0
            End Try
        End Set
    End Property

    Public Shared Sub AddPaymentMethode(payment As PaymentMethode, ByRef xml As IDatabase)
        xml.SaveOrUpdatePaymentMethode(payment)
    End Sub

    Public Shared Function GetByName(Name As String, ByRef xml As IDatabase) As PaymentMethode
        For Each Zahlung As PaymentMethode In xml.ListOfPaymentMethodes
            If Zahlung.Name = Name Then
                Return Zahlung
            End If
        Next
        Return Nothing
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim temp As PaymentMethode = obj
            If temp.Name = Name Then
                Return True
            End If
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    Protected Overridable Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
